package com.snow.vpnclient.util;

import android.os.Bundle;

import androidx.activity.OnBackPressedCallback;
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

/**
 * @author liufq
 * @since 2022-10-25 13:58:42
 */
public class FragmentUtils {

    public static void action(Fragment fragment, int resId) {
        NavHostFragment.findNavController(fragment).navigate(resId);
    }

    public static void action(Fragment fragment, int resId, Bundle args) {
        NavHostFragment.findNavController(fragment).navigate(resId, args);
    }

    public static void back(Fragment fragment) {
        NavHostFragment.findNavController(fragment).popBackStack();
    }

    /**
     * 设置返回后台打开
     */
    public static void backOn(Fragment fragment) {
        fragment.requireActivity().getOnBackPressedDispatcher()
                .addCallback(fragment, new OnBackPressedCallback(true) {
                    @Override
                    public void handleOnBackPressed() {
                        fragment.getActivity().moveTaskToBack(false);
                    }
                });
    }
}
